<?php
include_once ('class.mssql.php');

class EditResponsible
{
    private $comtec_id_responsible;
   
    private $name;
    private $lastname1;
    private $lastname2;
    private $rut;
    private $position;
    private $tag_id_responsible;
    private $birthdate;
    private $notes;
    private $cod_mainteiner;
    private $company_income_date;

    private $errors;
    private $token;

    public function __construct()
    {
        $this->errors   = array();
        
        $this->comtec_id_responsible = $_POST['comtec_id_responsible'];
        
        $this->tag_id_responsible = $_POST['tag_id_responsible'];
        $this->name         = $_POST['name'];
        $this->lastname1    = $_POST['lastname1'];
        $this->lastname2    = $_POST['lastname2'];
        $this->rut          = $_POST['rut'].$_POST['rut_cod'];
        $this->position	    = $_POST['position'];
        //TODO validar: que la fecha de entrada llegue completa
        $this->company_income_date = $_POST['income_year'].$_POST['income_month'].$_POST['income_day']." 00:00:00";
        //TODO validar: que la fecha de cumpleaños llegue completa
        $this->birthdate    = $_POST['birthdate_year'].$_POST['birthdate_month'].$_POST['birthdate_day']." 00:00:00";
        $this->notes        = $_POST['notes'];
        if(strlen($this->notes) > 400)
        {
         $this->notes = substr($this->notes,0,400);   
        }
        $this->cod_mainteiner = $_POST['cod_mainteiner'];

        $this->token    = $_POST['token'];
    }
    
    public function getName()
    {
     return $this->name;   
    }
    
    public function getLastname1()
    {
     return $this->lastname1;
    }

    public function process()
    {
        if($this->valid_token() && $this->valid_data())
            $this->edit();

        return count($this->errors)? 0 : 1;
    }

    public function filter($var)
    {
        return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
    }

    public function filter_num($var)
    {
        return preg_replace('/[^0-9+]/','',$var);
    }

    public function edit()
    {
        $db = new MSSQL();
        
        $tsql  = "UPDATE responsible SET ";
	$tsql .= "name= ? , ";
        $tsql .= "lastname1= ? , ";
        $tsql .= "lastname2= ? , ";
        $tsql .= "rut= ? , ";
        $tsql .= "tag_id_responsible= ? , ";
        $tsql .= "position = ? , ";                             
        $tsql .= "birthdate= ? , ";
        $tsql .= "notes= ? , ";
        $tsql .= "cod_mainteiner= ? , ";
        $tsql .= "company_income_date= ? ";                           
	$tsql .= " WHERE comtec_id_responsible= ? ";
        
        $params = array(&$this->name,&$this->lastname1,&$this->lastname2,&$this->rut,&$this->tag_id_responsible,&$this->position,
                        &$this->birthdate,&$this->notes,&$this->cod_mainteiner,&$this->company_income_date,&$this->comtec_id_responsible);
        $stmt = $db->prepare($tsql,$params);

        if(!sqlsrv_execute($stmt))
            $this->errors[] = 'No se puede procesar la edici&oacute;n';
    }

    public function show_errors()
    {
        $msg = "";
        foreach($this->errors as $key=>$value)
            $msg = $msg . $value . "\n";
        return $msg;
    }

    public function valid_data()
    {
        //if($this->user_exists())
          //  $this->errors[] = 'Direccion de correo electronico ya existe';

        return count($this->errors)? 0 : 1;
    }

    public function valid_token()
    {
        if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
            $this->errors[] = 'Registro inv&aacute;lido';

        return count($this->errors)? 0 : 1;
    }
}
?>
